<?php
namespace app\admin\controller;

use app\common\model\Config as ConfigModel;
use app\common\model\ConfigLog as ConfigLogModel;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\Admin as AdminModel;

/**
 * 系统配置
 * 系统配置 做好记录 比如是谁改的配置
 * Class Config
 * @package app\admin\controller
 */
class Config extends  Base{


    public function index(){

        $config = ConfigModel::all();
        $this->data['model'] = $config;
        $this->data['title'] =  '系统设置';

        return view('',$this->data);
    }


    /**
     * @return \think\response\view
     */
    public function add(){

        $this->data['title'] = '添加配置项';

        return view('',$this->data);
    }


    /**
     *
     * @param Request $request
     */
    public function create(Request $request){
        $post = $request->post();
        $validate = Loader::validate("Config");
        if(!$validate->scene('add')->check($post)){
            $this->error($validate->getError());
        }

        $count = ConfigModel::where('ckey',$post['ckey'])->count();
        if($count){
            $this->error('已经存在该配置项了');
        }

        $now = time();
        Db::startTrans();
        try{
            $model = new ConfigModel();
            $post['add_time'] = $post['update_time'] = $now;
            $model->data($post);
            if($model->allowField(true)->save()){
                $log_data = [];
                $log_data['ckey']     = $post['ckey'];
                $log_data['cvalue']   = '' ;
                $log_data['op_user']  = $this->admin->id;
                $log_data['type']     = 0 ;
                $log_data['add_time'] = $now;
                ConfigLogModel::create($log_data);
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error("操作失败");
        }

        $this->success('操作成功');
    }


    /**
     * @param Request $request
     */
    public function update(Request $request){
        $data = $request->post();
        $validate = Loader::validate('config');
        if(!$validate->scene('update')->check($data)){
            $this->error($validate->getError());
        }

        Db::startTrans();
        try{
            $model = ConfigModel::where('ckey',$data['ckey'])->find();
            $res = $model->save($data);
            if($res){
                ConfigLogModel::create([
                    'ckey'=>$data['ckey'],
                    'cvalue' => $data['cvalue'],
                    'op_user' => $this->admin->id,
                    'type' => 1,
                    'add_time' => time()
                ]);
            }
            Db::commit();
        }catch ( \Exception $e ){
            Db::rollback();
            $this->error("操作失败");
        }
        $this->success('更新成功');
    }

    /**
     * @param Request $request
     * @throws \think\exception\DbException
     */
    public function delete(Request $request){
        $ckey = $request -> post('ckey');
        $model = ConfigModel::where('ckey',$ckey)->find();
        Db::startTrans();
        try{
            $log_data = [];
            $log_data['ckey'] = $model->ckey;
            $log_data['op_user'] = $this->admin->id;
            $log_data['type'] = 2;
            $log_data['add_time'] = time();
            $res = $model->delete();
            if($res){
                ConfigLogModel::create($log_data);
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error("删除成功");
        }

        $this->success("删除成功");
    }


    /**
     *配置修改的历史
     */
    public function history(){

       $history = ConfigLogModel::where([])->order('id desc')->paginate(10);
       $op_user = [];
       foreach( $history as $key => $val ){
           array_push($op_user,$val['op_user']);
       }
       $op_user = array_unique($op_user);
       $op_users = AdminModel::where('id','in',$op_user)->column('id,nickname','id');

       $this->data['model'] = $history;
       $this->data['title'] = '配置操作历史';
       $this->data['op_user']  = $op_users;

       return view('',$this->data);
    }
}